<template>
  <div class="detail">
    <Navi title="商品详情" :fixed="true" />
    <div>
      <div class="detail-img">
        <img class="smallImg" :src="productDetail.imgurl" alt="" />
      </div>

      <div class="detail-info">
        <div class="detail-pro">
          <div>
            <div class="detail-name one-text">{{productDetail.name}}</div>
          </div>
          <div class="detail-price">&yen;{{productDetail.price}}</div>
        </div>

        <div class="select-count-box">
          <div class="select-text">选择数量</div>
          <div class="select-count">
            <van-stepper v-model="count" theme="round" button-size="22" disable-input />
          </div>
        </div>
        <div class="detail-desc">
          <div class="detail-desc-title">商品描述</div>
          <div class="detail-desc-content">
            <div class="desc-item" >{{productDetail.info}}</div>
          </div>
        </div>
      </div>
    </div>

    <van-action-bar>
      <van-action-bar-button type="warning" text="加入购物车" @click="addShopbag(this.productDetail.id,this.count)"/>
      <van-action-bar-button type="danger" text="去支付" @click="pay()"/>
    </van-action-bar>


  </div>
</template>

<script>
  import Navi from '../components/Navi.vue';
  export default {
    name: 'Detail',
    data() {
      return {
        count: 1,
        id: '',
        productDetail: {},
        //购物袋数量
        shopbagCount: ''
      };
    },
    components: {
      Navi
    },

    created() {
      this.id = this.$route.params.id;
      this.getProductDetailById(this.id);
    },

    methods: {
      //根据商品id获取商品详情数据
      getProductDetailById(id) {
        this.axios({
          method: 'get',
          url: '/teagoods/selOne.do?id='+id,
        }).then(result => {
          if (result.data.code === 200) {
            let data = result.data.data;
            this.productDetail = data;
          }
        })
      },
      //加入购物袋
      addShopbag(goodsId,goodsNum) {
        this.$toast(this.id+"+"+this.productDetail.id);
        this.axios({
          method: 'post',
          url: '/shopcar/addGoods.do',
          headers: {
            'Content-Type': 'application/json'
          },
          params: {
            goodsId,
            goodsNum
          }
        }).then(result => {
          if (result.data.code === 200) {
            this.$toast('加入购物袋成功');
            // this.shopbagCount = Number(this.shopbagCount) + this.count;
          } else {
            this.$toast('加入购物袋失败');
          }
        }).catch(err => {

        })
      },
      pay(){
        this.$router.push('/shopBag')
      }


    }
  }
</script>

<style lang="less" scoped>
  .detail{
    padding-bottom: 50px;
    padding-top: 46px;
    .desc-item{
      margin-bottom: 10px;
      &:last-child{
        margin-bottom: 0;
      }
    }
    .detail-desc-content{
      color: #666;
    }
    .detail-desc-title{
      font-size: 14px;
      margin-bottom: 10px;
    }
    .detail-desc{
      margin-top: 20px;
    }
    .select-text{
      font-size: 14px;
      color: #666;
    }
    .select-count{
      margin-left: auto;
    }
    .select-count-box{
      padding: 15px 0;
      margin-top: 20px;
      border-top: 1px solid #f5f5f5;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      align-items: center;
    }
    .rules-item{
      text-align: center;
      width: 70px;
      line-height: 30px;
      height: 30px;
      background-color: #e8e8e8;
      margin-right: 10px;
      border-radius: 16px;
      &:last-child{
        margin-right: 0;
      }
      &.active{
        background-color: #3762f0;
        color: #fff;
      }
    }
    .rules-title{
      width: 50px;
      height: 30px;
      line-height: 30px;
      font-size: 14px;
    }
    .rules{
      display: flex;
      font-size: 14px;
    }
    .detail-rules-item{
      display: flex;
      margin-bottom: 10px;
    }
    .detail-rules{
      margin-top: 20px;
    }
    .detail-price{
      font-size: 16px;
      font-weight: bold;
      color: #e4393c;
    }
    .detail-name{
      font-size: 14px;
    }
    .detail-enname{
      color: #999;
      font-size: 12px;
    }
    .detail-price{
      margin-left: auto;
    }
    .detail-pro{
      display: flex;
      align-items: center;
    }
    .detail-info{
      padding: 10px;
      background-color: #fff;
      margin: 0 10px;
      position: relative;
      top: -10px;
      border-top-left-radius: 10px;
      border-top-right-radius: 10px;
    }
    .detail-img{
      background-color: #ddd;
    }
    .smallImg{
      width: 100%;
      height: 100%;
    }
  }
</style>